package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.common.Result;
import com.atguigu.spzx.model.entity.sys.SysMenu;
import com.atguigu.spzx.model.vo.sys.MenuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "菜单管理模块")
@RequestMapping("/admin/system/menu")
public class SysMenuController {

    @Resource
    SysMenuService sysMenuService;
    @Operation(summary = "查询登录用户的动态菜单嵌套集合")
    @GetMapping("findDynamicMenus")
    public Result findDynamicMenus(){
        List<SysMenu> menuVos = sysMenuService.findDynamicMenus();
        return Result.ok().data(menuVos);
    }

    @Operation(summary = "查询菜单的嵌套集合")
    @GetMapping("findNestdMenus")
    public Result findNestdMenus(){
        //前端需要的数据：后端是否需要自定义VO类
        //SysMenu： 每行数据都对应一个SysMenu对象，但是前端需要展示父子关系的菜单集合
        //在SysMenu中添加List<SysMenu>children属性 ，前端判断如果children不为空 表示有子菜单 可以自动展示
        List<SysMenu> parentMenus = sysMenuService.findNestdMenus();//查询返回组装好的一级菜单集合
        return Result.ok().data(parentMenus);
    }
    @Operation(summary = "删除指定菜单")
    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable("id")Long id){
        sysMenuService.deleteById(id);
        return Result.ok();
    }
    @Operation(summary = "查询指定菜单")
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable("id")Long id){
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok().data(sysMenu);
    }

    @Operation(summary = "更新菜单")
    @PutMapping("updateById")
    public Result updateById(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return Result.ok();
    }
    // 前端提交新增菜单数据 需要绑定parent_id
    @Operation(summary = "新增下级菜单")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return Result.ok();
    }
}
